package org.go.work.self;

import java.util.List;

import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import org.go.Go;
import org.go.GoContext;
import org.go.domain.MailInfo;
import org.go.expcetion.GoException;
import org.go.expcetion.WorkExecutionException;
import org.go.log.Log;
import org.go.log.LoggerFactory;
import org.go.work.WorkDataMap;

/**
 * 
 * @author hejie
 *
 */
public class SendMailWork implements Go {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1639640143227847574L;

	private static final Log log = LoggerFactory.getLog(SendMailWork.class);

	private Object createMailInfo() {
		return null;
	}

	private MailInfo populateMailInfo(WorkDataMap data, Object createMailInfo) {
		return null;
	}

	private MimeMessage prepareMimeMessage(MailInfo mailInfo) {
		return null;
	}

	@SuppressWarnings("finally")
	@Override
	public Boolean doWork(GoContext context) throws WorkExecutionException {
		WorkDataMap data = context.getMergedJobDataMap();

		MailInfo mailInfo = populateMailInfo(data, createMailInfo());

		log.info("Sending message {}", mailInfo);

		try {
			MimeMessage mimeMessage = prepareMimeMessage(mailInfo);
			Transport.send(mimeMessage);
		} catch (MessagingException e) {
			throw new WorkExecutionException("Unable to send mail: " + mailInfo, e);
		} finally {
			return true;
		}
	}

	@Override
	public List selectWork(String taskParameter, String ownSign, int taskItemNum, List taskItemList, int eachFetchDataNum) throws GoException {
		// TODO Auto-generated method stub
		return null;
	}
}
